package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class MachineCollection here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MachineCollection
{
    private static ArrayList<Machine> machines;
    private static FileMachine file;
    private static MachineCollection instance;

    /**
     * Constructor for objects of class MachineCollection
     */
    protected MachineCollection()
    {
        file = new FileMachine();
        machines = file.get_data();
    }
    
    public static void initialize()
    {
        if(instance == null)
        {
            instance = new MachineCollection();
        }
    }
    
    public static MachineCollection get_instance()
    {
        return instance;
    }
    
    public static boolean add_machine(String name, double price_per_day)
    {
        int max_temp_id = 0;
        int temp_id = 0;
        String id;
        for (Machine machine : machines)
        {
            temp_id = Integer.parseInt(machine.get_id());
            if(max_temp_id < temp_id)
            {
                if(max_temp_id < (temp_id - 1))
                {
                    if(!(is_such_id(id = "" + (max_temp_id + 1))))
                    {
                        break;
                    }
                }
                max_temp_id = temp_id;     
            }
        }
        id = "" + (max_temp_id + 1);           
        machines.add(max_temp_id, new Machine(id, name, price_per_day, false));
        return true;
    }
    
    public static boolean save_data()
    {
        return file.save_changes(machines);
    }
    
    public static void edit_machine(String id, String name, double price_per_day)
    {
        Machine temp = find_by_id(id);
        temp.set_name(name);
        temp.set_price_per_day(price_per_day);
    }
    
    public static boolean delete_machine(String id)
    {
        return machines.remove(find_by_id(id));
    }
    
    public static String display_machine(String id)
    {
        return find_by_id(id).printable();
    }
    
    public static String display_all_machines()
    {
        String returner = "";
        for(Machine machine : machines)
        {
            if(!(machine.is_unfinished()))
            {
                returner += "\n" + machine.printable();
            }
        }
        return returner;
    }
    
    public static Machine find_by_id(String id)
    {
        for (Machine machine : machines)
        {
            if (machine.get_id().equals(id))
            {
                return machine;
            }
        }
        return null;
    }
    
    public static boolean is_such_id(String id)
    {
        for(Machine machine : machines)
        {
            if (machine.get_id().equals(id))
            {
                return true;
            }
        }
        return false;
    }
    
    public static boolean is_leased(String id)
    {
        return find_by_id(id).get_is_leased();
    }
    
    public static boolean is_unfinished(String id)
    {
        return find_by_id(id).is_unfinished();
    }
    
    public static String display_unfinished()
    {
        String all_data = "";
        for (Machine machine : machines)
        {
            if (machine.is_unfinished())
            {
                all_data += machine.printable() + "\n";
            }
        }
        return all_data;
    }
    
    public static void finish_machine(String id, double price_per_day)
    {
        find_by_id(id).set_price_per_day(price_per_day);
    }
    
    public static boolean are_there_any_unfinished()
    {
        for (Machine machine : machines)
        {
            if(machine.is_unfinished())
            {
                return true;
            }
        }
        return false;
    }
    
    

 
}
